<?php

/**
 * 首页美食分享类目推荐
 * 
 * @copyright dongping
 * @since 2012-03-20
 */


include_once($_SERVER["DOCUMENT_ROOT"] . "/core/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/core/admin.inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/include/foodv2/inc.php");

$g_cgival = array();
$g_pro = array();
$g_show = array();


function check_cgi_pro()
{
	global $g_cgival, $g_pro, $g_show;
	
	
	cgi::get($g_cgival["class"], "class", "");
	$g_cgival["class"] = string::un_script_code($g_cgival["class"]);
	$g_cgival["class"] = string::un_html($g_cgival["class"]);
	
	if ($g_cgival["class"] !== "菜系"
		and $g_cgival["class"] !== "食尚"
		and $g_cgival["class"] !== "商圈"
		and $g_cgival["class"] !== "小吃"
		and $g_cgival["class"] !== "秀美食"
		and $g_cgival["class"] !== "专辑")
	{
		$g_cgival["class"] = "菜系";
	}
	
	/**
	 * 搜索参数设置
	 * add by zhanghuan at 2012/05/17
	 */
	cgi::get($g_cgival['tag_name'],"tag_name",'');
	$g_cgival['starttime'] = string::un_html($g_cgival['tag_name']);
	
	cgi::get($g_cgival['tag_index_tj'],"tag_index_tj",'');
	$g_cgival['tag_index_tj'] = string::un_html($g_cgival['tag_index_tj']);
	
	// 分页
	cgi::get($g_cgival["p"], "p", 1);
	cgi::get($g_cgival["pcount"], "pcount", 20);
	$g_cgival["p"] = intval($g_cgival["p"]);
	$g_cgival["pcount"] = intval($g_cgival["pcount"]);	
}

function get_data()
{
	global $g_cgival, $g_pro, $g_show;
	
	$tag_class = $g_cgival["class"];
	$p = $g_cgival["p"];
	$pcount = $g_cgival["pcount"];
	$g_show['tag_name'] = $g_cgival['tag_name'];
	$g_show['tag_index_tj'] = $g_cgival['tag_index_tj'];
	
	$g_show["class"] = $tag_class;
	

	// 拿到当前站点的site_id
	$site_id = admin::admin_get_user_site_id();
	
	
	$db = new db_food_tag();
	$db->debug = true;
	
	
	// 获取当前class 下所有的数据
	$data = $db->admin_get_list_by_tag_class($site_id, $tag_class, "tag_index_tj desc , id desc", $p, $pcount, $g_show['tag_name'], $g_show['tag_index_tj']);
	$count = $db->admin_get_count_by_tag_class($site_id, $tag_class, $g_show['tag_name'], $g_show['tag_index_tj']);
	$g_show["count"] = $count;
	if (is_array($data))
	{
		$g_show["data"] = $data;
	}
	else
	{
		$g_show["data_none"] = 1;
	}

	
	// Pager
	$max_page = page_bar::get_page_max($g_show["count"], $g_cgival['pcount'], $g_cgival["p"]);
	
	$page_bar_style = array(
			"curt"		=> $g_cgival["p"],
			"max"		=> $max_page,
			"key"		=> "p",
	);
	
	$g_show["page_bar"] = "<b>{$g_show["count"]}</b> 条记录&nbsp;&nbsp;第{$g_cgival["p"]}页&nbsp;共{$max_page}页&nbsp;" . page_bar::bar2($page_bar_style);

	
	
	
	// 获取已经上架的所有TAG
	$shangjia_data = $db->admin_get_index_tj($site_id,$tag_class);
	$g_show["food_tag_tj_data"] = $shangjia_data;
}

try
{
	admin_check_login();
// 	admin_check_user_priv("foodv2/main_nav.php");
	
	check_cgi_pro();
	get_data();
}
catch(Exception $e)
{
	$g_show["error"] = 1;
	$g_show["errmsg"] = $e->getMessage();
}

show_pro();
exit;


function show_pro()
{
	global $g_show;
	$smarty = p_get_smarty ();
	$smarty->assign ( 'g_show', $g_show );
	
	if (isset($_GET["debug"]))
	{
		echo "<pre>";
		print_r($g_show);
	}
	$smarty->display ( "foodv2/index/food_tag_tj.html" );
}

?>